﻿Public Class IncDec

    Private WithEvents timer As New Timer

    Private lblInput As TenKeyLabel

    Public Sub SetLabel(ByRef InputLabel As TenKeyLabel)
        lblInput = InputLabel
        If Not lblInput.IsEditing Then
            lblInput.BeginEdit()
        End If
        ResetTimer
    End Sub

    Public Sub ClearLabel()
        lblInput.EndEdit()
        timer.Stop()
    End Sub

    Private Sub IncDec_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.EnabledChanged
        If Not Me.Enabled Then
            timer.Stop()
        End If
    End Sub

    Private Sub IncDec_FontChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.FontChanged
        IncDec_SizeChanged(Me, New EventArgs)
    End Sub

    Private Sub IncDec_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        timer.Interval = 1500
    End Sub

    Private Sub IncDec_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        btnDec.Width = Me.Width
        btnInc.Width = Me.Width
        btnInc.Height = Me.Height / 2
        btnDec.Height = Me.Height / 2
        btnInc.Top = 0
        btnDec.Top = Me.Height / 2
        btnInc.Left = 0
        btnDec.Left = 0
    End Sub

    Private Sub btnInc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInc.Click
        If IsNothing(lblInput) Then
            Exit Sub
        End If

        If IsNumeric(lblInput.Text) Then
            lblInput.Text = CInt(lblInput.Text) + 1
        Else
            lblInput.Text = 1
        End If
        ResetTimer()
    End Sub

    Private Sub btnDec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDec.Click
        If IsNothing(lblInput) Then
            Exit Sub
        End If

        If IsNumeric(lblInput.Text) Then
            lblInput.Text = CInt(lblInput.Text) - 1
            If CInt(lblInput.Text) < 0 Then
                lblInput.Text = 0
            End If
        Else
            lblInput.Text = 0
        End If
        ResetTimer()
    End Sub

    Private Sub timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer.Tick
        Me.Enabled = False
        lblInput.EndEdit()
        timer.Stop()
    End Sub

    Private Sub ResetTimer()
        timer.Stop()
        timer.Interval = 1500
        timer.Start()
    End Sub
End Class
